/*
 * 文件名：RepairLogService.java	 
 * 时     间：下午1:38:46
 * 作     者：lyf      
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.threads.DeleteSetRuleRunble;
import com.cloud.threads.SetRuleCopyRunble;
import com.cloud.threads.SetRuleRunble;
import com.cloud.threads.UpDateCarOutInfoRunble;
import com.cloud.threads.UpDateCouponCashRunble;
import com.cloud.threads.UpDateCrossingRunble;
import com.cloud.threads.UpLoadCarOutInfoRunble;
import com.cloud.threads.UpdateStationRunble;
import com.cloud.threads.UploadAdminCarInBillRunble;
import com.cloud.threads.UploadAdminPayBillRunble;
import com.cloud.threads.UploadCarInInfoRunble;
import com.cloud.threads.UploadCouponRunble;
import com.cloud.threads.UploadCrossingAssignStationRunble;
import com.cloud.threads.UploadCrossingRunble;
import com.cloud.threads.UploadEquipmentRunble;
import com.cloud.threads.UploadMemberInfoRunble;
import com.cloud.threads.UploadPayBillRunble;
import com.cloud.threads.UploadStationRunble;
import com.cz073586.tools.util.Log4jUtil;
import com.cz073586.tools.util.ThreadPoolUtil;
import com.cz073586.tools.util.Tools;
import com.local.common.LogInfoConstan;
import com.local.dao.hismappers.LocalPayBillMapper;
import com.local.dao.mapers.AdminCarInBillMapper;
import com.local.dao.mapers.AdminPayBillMapper;
import com.local.dao.mapers.CarParkInMapper;
import com.local.dao.mapers.CarParkOutMapper;
import com.local.dao.mapers.CrossingMapper;
import com.local.dao.mapers.CrossingToStationMapper;
import com.local.dao.mapers.LocalChargeBytimeCopyMapper;
import com.local.dao.mapers.LocalChargeBytimeMapper;
import com.local.dao.mapers.LocalCouponCashMapper;
import com.local.dao.mapers.LocalCouponMapper;
import com.local.dao.mapers.LocalEquipmentMapper;
import com.local.dao.mapers.LocalMemberMapper;
import com.local.dao.mapers.LocalStationMapper;
import com.local.dao.mapers.LogMapper;
import com.local.manager.cfgManager.CloudConfigManager;
import com.local.model.AdminCarInBill;
import com.local.model.AdminPayBill;
import com.local.model.CarParkIn;
import com.local.model.CarParkOut;
import com.local.model.Crossing;
import com.local.model.CrossingToStation;
import com.local.model.CrossingToStationExample;
import com.local.model.LocalChargeBytime;
import com.local.model.LocalChargeBytimeCopy;
import com.local.model.LocalCoupon;
import com.local.model.LocalCouponCash;
import com.local.model.LocalEquipment;
import com.local.model.LocalMember;
import com.local.model.LocalPayBill;
import com.local.model.LocalStation;
import com.local.model.Log;
import com.local.service.face.LogService;
import com.qianhai.common.exception.BaseException;
import com.schedule.log.RepairLog;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: RepairLogService
 * @author lyf
 * @date 2015年11月26日 下午1:38:46
 */
@Service
public class RepairLogService implements RepairLog
{
	
	private static Log4jUtil log = Log4jUtil.getLog4j(RepairLogService.class);
	@Autowired
	private LogMapper logMapper;
	@Autowired
	private LogService logServier;
	@Autowired
	private CarParkInMapper carInMapper;
	@Autowired
	private CarParkOutMapper carOutMapper;
	@Autowired
	private LocalPayBillMapper localPayBillMapper;
	@Autowired
	private LocalCouponMapper localCouponMapper;
	@Autowired
	private AdminPayBillMapper adminPayBillMapper;
	@Autowired
	private AdminCarInBillMapper adminCarInBillMapper;
	@Autowired
	private LocalEquipmentMapper localEquipmentMapper;
	@Autowired
	private CrossingMapper crossingMapper;
	@Autowired
	private LocalCouponCashMapper localCouponCashMapper;
	@Autowired
	private LocalStationMapper localStationMapper;
	@Autowired
	private CrossingToStationMapper CrossingToStationMapper;
	@Autowired
	private LocalChargeBytimeMapper localChargeBytimeMapper;
	@Autowired
	private LocalChargeBytimeCopyMapper localChargeBytimeCopyMapper;
	@Autowired
	private LocalMemberMapper localMemberMapper;
	
	@Override
	public Integer rePainLog(Log logInfi) throws BaseException
	{
		try
		{
			
			int modueId = logInfi.getModule();
			
			// 避开断网修复
			if (modueId == 0)
			{
				return 1;
			}
			switch (modueId)
			{
			case LogInfoConstan.MODULE_CAR_IN: // 上传进场纪录
				
				CarParkIn carInInfo = carInMapper.selectByPrimaryKey(logInfi
						.getBusinessUuid());
				if (!Tools.isEmpty(carInInfo))
				{
					UploadCarInInfoRunble upCarInRunble = new UploadCarInInfoRunble(
							carInInfo, logServier);
					ThreadPoolUtil.instance().execute(upCarInRunble);
				}
				
				break;
			case LogInfoConstan.MODULE_CAR_OUT: // 出场失败
				CarParkOut carOut = carOutMapper.selectByPrimaryKey(logInfi
						.getBusinessUuid());
				if (!Tools.isEmpty(carOut))
				{
					UpLoadCarOutInfoRunble carOutRunble = new UpLoadCarOutInfoRunble(
							carOut, logServier);
					ThreadPoolUtil.instance().execute(carOutRunble);
				}
				break;
			case LogInfoConstan.MODULE_CAR_OUT_UPDATE: // 更新出场信息
				
				CarParkOut carOutUp = carOutMapper.selectByPrimaryKey(logInfi
						.getBusinessUuid());
				if (!Tools.isEmpty(carOutUp))
				{
					UpDateCarOutInfoRunble carOutRunble = new UpDateCarOutInfoRunble(
							carOutUp, logServier);
					ThreadPoolUtil.instance().execute(carOutRunble);
				}
				break;
			case LogInfoConstan.MODULE_PAYBILL: // 上传支付流水纪录
				
				LocalPayBill parkwellPayBill = localPayBillMapper
						.selectByPrimaryKey(logInfi.getBusinessUuid());
				if (!Tools.isEmpty(parkwellPayBill))
				{
					UploadPayBillRunble payBillRunble = new UploadPayBillRunble(
							parkwellPayBill, logServier);
					ThreadPoolUtil.instance().execute(payBillRunble);
				}
				break;
			case LogInfoConstan.MODULE_COUPON: // 上传优惠券纪录
				
				LocalCoupon localCoupon = localCouponMapper
						.selectByPrimaryKey(logInfi.getBusinessUuid());
				if (!Tools.isEmpty(localCoupon))
				{
					UploadCouponRunble couponRunble = new UploadCouponRunble(
							localCoupon, logServier);
					ThreadPoolUtil.instance().execute(couponRunble);
				}
				break;
			case LogInfoConstan.MODULE_ADMIN_PAY_BILL: // 上传强制开闸纪录
				
				AdminPayBill adminPayBill = adminPayBillMapper
						.selectByPrimaryKey(logInfi.getBusinessUuid());
				if (!Tools.isEmpty(adminPayBill))
				{
					UploadAdminPayBillRunble adminPayRunble = new UploadAdminPayBillRunble(
							adminPayBill, logServier);
					ThreadPoolUtil.instance().execute(adminPayRunble);
				}
				break;
			case LogInfoConstan.MODULE_ADMIN_CARIN_BILL: // 上传入场手动开闸纪录
				
				AdminCarInBill adminCarInBill = adminCarInBillMapper
						.selectByPrimaryKey(logInfi.getBusinessUuid());
				if (!Tools.isEmpty(adminCarInBill))
				{
					UploadAdminCarInBillRunble adminCarInRunble = new UploadAdminCarInBillRunble(
							adminCarInBill, logServier);
					ThreadPoolUtil.instance().execute(adminCarInRunble);
				}
				break;
			case LogInfoConstan.MODULE_EQUIPMENT: // 上传入场手动开闸纪录
				
				LocalEquipment equipment = localEquipmentMapper
						.selectByPrimaryKey(logInfi.getBusinessUuid());
				if (!Tools.isEmpty(equipment))
				{
					UploadEquipmentRunble equipmentRunble = new UploadEquipmentRunble(
							equipment, logServier);
					ThreadPoolUtil.instance().execute(equipmentRunble);
				}
				break;
			case LogInfoConstan.MODULE_CROSSING: // 上传路口纪录
				
				Crossing crossing = crossingMapper.selectByPrimaryKey(logInfi
						.getBusinessUuid());
				if (!Tools.isEmpty(crossing))
				{
					UploadCrossingRunble crossingRunble = new UploadCrossingRunble(
							crossing, logServier);
					ThreadPoolUtil.instance().execute(crossingRunble);
				}
				break;
			case LogInfoConstan.MODULE_CROSSING_UPDATE: // 修改路口纪录
				
				Crossing upcrossing = crossingMapper.selectByPrimaryKey(logInfi
						.getBusinessUuid());
				if (!Tools.isEmpty(upcrossing))
				{
					UpDateCrossingRunble updcrossingRunble = new UpDateCrossingRunble(
							upcrossing, logServier);
					ThreadPoolUtil.instance().execute(updcrossingRunble);
				}
				break;
			case LogInfoConstan.MODULE_COUPON_CASH_UPDATE: // 上传商家电子抵用卷修改纪录
				
				LocalCouponCash localCouponCash = localCouponCashMapper
						.selectByPrimaryKey(logInfi.getBusinessUuid());
				if (!Tools.isEmpty(localCouponCash))
				{
					UpDateCouponCashRunble couponCashRunble = new UpDateCouponCashRunble(
							localCouponCash, logServier);
					ThreadPoolUtil.instance().execute(couponCashRunble);
				}
				break;
			case LogInfoConstan.MODULE_STATION: // 上传岗亭纪录
				
				LocalStation localStation = localStationMapper
						.selectByPrimaryKey(logInfi.getBusinessUuid());
				if (!Tools.isEmpty(localStation))
				{
					UploadStationRunble stationCashRunble = new UploadStationRunble(
							localStation, logServier);
					ThreadPoolUtil.instance().execute(stationCashRunble);
				}
				break;
			case LogInfoConstan.MODULE_STATION_UPDATE: // 修改岗亭纪录
				
				LocalStation upLocalStation = localStationMapper
						.selectByPrimaryKey(logInfi.getBusinessUuid());
				if (!Tools.isEmpty(upLocalStation))
				{
					UpdateStationRunble updStationCashRunble = new UpdateStationRunble(
							upLocalStation, logServier);
					ThreadPoolUtil.instance().execute(updStationCashRunble);
				}
				break;
			case LogInfoConstan.MODULE_CROSSING_STATION_UPDATE: // 修改路口与岗亭的关联纪录
				CrossingToStationExample contdion = new CrossingToStationExample();
				contdion.or().andCrossingUuidEqualTo(logInfi.getBusinessUuid());
				List<CrossingToStation> li = CrossingToStationMapper
						.selectByCondition(contdion);
				
				if (!Tools.isEmpty(li))
				{
					UploadCrossingAssignStationRunble crossingAssignStationRunble = new UploadCrossingAssignStationRunble(
							li, logServier);
					ThreadPoolUtil.instance().execute(
							crossingAssignStationRunble);
				}
				break;
			case LogInfoConstan.RULE_UPLOAD:
				LocalChargeBytime localChargeBytime=localChargeBytimeMapper.selectByPrimaryKey(logInfi
						.getBusinessUuid());//上传计费规则
				if (CloudConfigManager.instance().isOpen())
				{
					SetRuleRunble upload = new SetRuleRunble(
							localChargeBytime, logServier);
					ThreadPoolUtil.instance().execute(upload);
				}
				break;
			case LogInfoConstan.RULE_COPY_UPLOAD:
				LocalChargeBytimeCopy localChargeBytimeCopy=localChargeBytimeCopyMapper.selectByPrimaryKey(logInfi
						.getBusinessUuid());
				if (CloudConfigManager.instance().isOpen())
				{
					SetRuleCopyRunble upload = new SetRuleCopyRunble(
							localChargeBytimeCopy, logServier);
					ThreadPoolUtil.instance().execute(upload);
				}
				break;
			case LogInfoConstan.REPORT_CAR_INFO:
				//进场
				if(logInfi.getDescript().equalsIgnoreCase("0")){
					CarParkIn carInInfocc = carInMapper.selectByPrimaryKey(logInfi
							.getBusinessUuid());
					
					if (!Tools.isEmpty(carInInfocc))
					{
						UploadCarInInfoRunble upCarInRunble = new UploadCarInInfoRunble(
								carInInfocc, logServier);
						ThreadPoolUtil.instance().execute(upCarInRunble);
					}
				}else {
					//出场
					CarParkOut carOutcc = carOutMapper.selectByPrimaryKey(logInfi
							.getBusinessUuid());
					
					if (!Tools.isEmpty(carOutcc))
					{
						UpLoadCarOutInfoRunble carOutRunble = new UpLoadCarOutInfoRunble(
								carOutcc, logServier);
						ThreadPoolUtil.instance().execute(carOutRunble);
					}
				}
				break;
			case LogInfoConstan.MEMBER_INFO:
				//查询当前车辆，是否已经存在与会员信息中
				List<LocalMember> lm=localMemberMapper.findParkwellMemberByCarNo(logInfi
						.getBusinessUuid());
				//会员车主信息，上传到云平台
				if (CloudConfigManager.instance().isOpen())
				{
					if(Tools.isEmpty(lm) && lm.isEmpty())
					{
						return 0;
					}
					UploadMemberInfoRunble upload = new UploadMemberInfoRunble(
							lm.get(0), logServier);
					ThreadPoolUtil.instance().execute(upload);
				}
				break;
			case LogInfoConstan.DELETERULE:
				List<String> ids=new ArrayList<String>();
				ids.add(logInfi.getBusinessUuid());
				if (CloudConfigManager.instance().isOpen())
				{
					DeleteSetRuleRunble upload = new DeleteSetRuleRunble(
							ids, logServier);
					ThreadPoolUtil.instance().execute(upload);
				}
				break;
			}
			// logInfi.setStatus(2);// 标记修复
			logMapper.deleteByPrimaryKey(logInfi.getUuid());
		} catch (Throwable t)
		{
			log.error(t.getMessage(), t);
			throw new BaseException(t.getMessage());
		}
		return null;
	}
	
}
